package zp.weibo;

import java.util.List;

import zp.weibo.util.Comment;
import zp.weibo.util.Message;
import zp.weibo.util.WeiboUtil;
import android.app.Activity;
import android.os.Bundle;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.View;
import android.view.GestureDetector.SimpleOnGestureListener;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.ViewFlipper;

public class WeiboActivity extends Activity {
	/** Called when the activity is first created. */

	// UI元素
	private TextView view_title;
	private TextView view_comments;
	private EditText text_newcomm;
	private Button button_comm;
	private ViewFlipper viewFlipper;

	// 获取的消息列表
	private List<Message> msgs;

	// 当前消息的评论列表
	private List<Comment> cms;

	private int cur = 0;
	
	// 滑动参数
	private static final int SWIPE_MIN_DISTANCE = 120;
    private static final int SWIPE_MAX_OFF_PATH = 250;
	private static final int SWIPE_THRESHOLD_VELOCITY = 200;
	
	private GestureDetector gestureDetector;
	View.OnTouchListener gestureListener;
//	private Animation slideLeftIn;
//	private Animation slideLeftOut;
//	private Animation slideRightIn;
//    private Animation slideRightOut;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		initShow();
		initData();
		getContent();
		
	}

	/**
	 * 获取界面元素
	 */
	private void initShow() {
		view_title = (TextView) findViewById(R.id.title);
		view_comments = (TextView) findViewById(R.id.comments);
		text_newcomm = (EditText)findViewById(R.id.newcomm);
		button_comm = (Button) findViewById(R.id.comm);
		viewFlipper = (ViewFlipper) findViewById(R.id.flipper);

		button_comm.setOnClickListener(pub);
		
//		slideLeftIn = AnimationUtils.loadAnimation(this, R.anim.slide_left_in);
//        slideLeftOut = AnimationUtils.loadAnimation(this, R.anim.slide_left_out);
//        slideRightIn = AnimationUtils.loadAnimation(this, R.anim.slide_right_in);
//        slideRightOut = AnimationUtils.loadAnimation(this, R.anim.slide_right_out);
        
        gestureDetector = new GestureDetector(new MyGestureDetector());
        gestureListener = new View.OnTouchListener() {
            public boolean onTouch(View v, MotionEvent event) {
                if (gestureDetector.onTouchEvent(event)) {
                    return true;
                }
                return false;
            }
        };
	}
	
	@Override
    public boolean onTouchEvent(MotionEvent event) {
        if (gestureDetector.onTouchEvent(event))
	        return true;
	    else
	    	return false;
    }
	
	class MyGestureDetector extends SimpleOnGestureListener {
        @Override
        public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
            try {
                if (Math.abs(e1.getY() - e2.getY()) > SWIPE_MAX_OFF_PATH)
                    return false;
                // right to left swipe
                if(e1.getX() - e2.getX() > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
//                	viewFlipper.setInAnimation(slideLeftIn);
//                    viewFlipper.setOutAnimation(slideLeftOut);
                    int max = msgs.size();
                    if(cur<max-1){
                    	cur++;
                    	getContent();
                    }
                	
                }  else if (e2.getX() - e1.getX() > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
//                	viewFlipper.setInAnimation(slideRightIn);
//                    viewFlipper.setOutAnimation(slideRightOut);
                    if(cur>0){
                    	cur--;
                    	getContent();
                    }
                	
                }
                
            } catch (Exception e) {
                // nothing
            }
            return false;
        }
    }
	
	private OnClickListener pub = new OnClickListener(){

		public void onClick(View v) {
			try{
				String id = msgs.get(cur).getId();
				WeiboUtil.pubComment(id, text_newcomm.getText().toString());
				getCommons(id);
				text_newcomm.setText("");
			}catch (Exception e) {
				
			}
			
		}
	};


	private void initData() {
		try {
			msgs = WeiboUtil.getContext();
		} catch (Exception e) {
			Toast.makeText(this, "无法获取信息", Toast.LENGTH_SHORT);
		}
	}

	private void getCommons(String msgid) {
		try {
			view_comments.setText("");
			cms = WeiboUtil.getComments(msgid);
			
			if (cms != null && 0 != cms.size()) {
				StringBuffer sb = new StringBuffer();
				int max = cms.size();
				for (int i = 0; i < max; i++) {
					Comment cm = cms.get(i);
					sb.append("\n\n  " + cm.getUser() + ":" + cm.getContent());
				}
				view_comments.setText(sb.toString());
			} else
				view_comments.setText("\n    无评论！");
			
		} catch (Exception e) {
			view_comments.setText("无法获取评论");
			Toast.makeText(this, "无法获取评论", Toast.LENGTH_SHORT);
		}
		
		
	}

	private void getContent() {

		if (null != msgs && 0 != msgs.size()) {
			Message msg = msgs.get(cur);
			view_title.setText(msg.getUser() + ":" + msg.getContent());
			String id = msg.getId();
//			Toast.makeText(this, id, Toast.LENGTH_SHORT);
			getCommons(id);
			
			
		}
		// try {
		// Bundle bundle = this.getIntent().getExtras();
		// msgs = (List<Message>)bundle.getSerializable("msgs");
		// cur = bundle.getInt("cur");
		// view_title.setText(String.valueOf(cur));
		// if (msgs == null)
		// view_comments.setText("It's null!");
		//			
		// else{
		// Message msg = msgs.get(0);
		// view_comments.setText(msg.getUser());
		// }
		//
		//				
		// } catch (Exception e) {
		// view_title.setText(e.toString());
		// }

	}

	
}